<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../../css/mui.min.css" rel="stylesheet" />
	</head>

	<style>
		input {
			color: gray;
		}
	</style>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-back  mui-icon-backmui-pull-left"></a>
			<h1 class="mui-title">请您修改</h1>
		</header>

		<div class="mui-content">
			<div class="mui-content-padded" style="margin: 5px;">
				<form class="mui-input-group">
					<div class="mui-input-row">
						<label>省</label>
						<input id="province" type="text" class="mui-input-clear">
					</div>
					<div class="mui-input-row">
						<label>市</label>
						<input id="city" type="text" class="mui-input-clear">
					</div>
					<div class="mui-input-row">
						<label>区</label>
						<input id="county" type="text" class="mui-input-clear">
					</div>
					<div class="mui-input-row">
						<label>详细地址</label>
						<input id="detailed" type="text" class="mui-input-clear">
					</div>
					<div class="mui-input-row">
						<label>联系人姓名</label>
						<input id="receivingName" type="text" class="mui-input-clear">
					</div>
					<div class="mui-input-row">
						<label>联系人手机</label>
						<input id="receivingPhone" type="text" class="mui-input-clear">
					</div>
					<div class="mui-input-row">
						<label>邮编</label>
						<input id="postCode" type="text" class="mui-input-clear">
					</div>
					<div class="mui-input-row">
						<label>设为默认</label>
						<div id="isDefault" class="mui-switch">
							<div class="mui-switch-handle"></div>
							<input id="isDefaultInput" type="number" value="" class="mui-hidden" />
						</div>
					</div>
					<input id="addressId" type="number" class="mui-hidden" />
					<div class="mui-button-row mui-button-block">
						<button id="delete" type="button" class="mui-btn mui-btn-danger" onclick="return false;">
							&nbsp;删除&nbsp;
						</button>
						<button id="quren" type="button" class="mui-btn mui-btn-primary" onclick="return false;">
							&nbsp;确认&nbsp;
						</button>
					</div>
				</form>
			</div>
		</div>
	</body>

	<script src="../../../js/mui.min.js"></script>
	<script src="../../../js/myjs/localVarchar.js"></script>
	<script src="../../../js/ipjs/ip.js"></script>
	<script src="../../../js/constatnt/var.js"></script>
	<script type="text/javascript">
		mui.init({})
		var addressId;
		mui.plusReady(function() {
			//接受返过来的数据信息对象
			var self = plus.webview.currentWebview();
			//获取其中的字段
			addressId = self.idValue; //获得参数
			document.getElementById("addressId").value = addressId;
			mui.ajax(ip + '/address/selectAddress/' + addressId, {
				data: addressId,
				type: 'get',
				success: function(data) {
					//获取json数据显示
					var address = data.data;
					document.getElementById("province").value = address.province;
					document.getElementById("city").value = address.city;
					document.getElementById("county").value = address.county;
					document.getElementById("detailed").value = address.detailed;

					isDefault = address.isDefault;
					if(isDefault === 1) {
						document.getElementById('isDefault').classList.add('mui-active');
					}
					document.getElementById("isDefaultInput").value = isDefault;
					document.getElementById("receivingName").value = address.receivingName;
					document.getElementById("receivingPhone").value = address.receivingPhone;
					document.getElementById("postCode").value = address.postCode;
				},
				//{"status":1000,"data":[{"id":1,"userId":1,"province":"广西壮族","city":"北京市","county":"东城区","detailed":"人陪我解压完","isInvalid":0,"isDefault":0,"postCode":785862,"receivingName":"兔兔路","receivingPhone":"17755810050","createTime":1534299323000,"updateTime":1534299323000}],"msg":"操作成功","respTime":"2018-08-15 10:40:41"}			
				error: function(xhr, type, errorThrown) {
					//异常处理；
					mui.toast("系统异常！");
				}
			});
		});
		mui('.mui-switch').each(function() {
			this.addEventListener('toggle', function(event) {
				var is = (this.classList.contains('mui-active') ? 'true' : 'false');
				if(is == "true") {
					document.getElementById("isDefaultInput").value = 1;
				} else {
					document.getElementById("isDefaultInput").value = 0;
				}
			});
		});
		document.getElementById("quren").addEventListener('tap', function() {
			var addressEntity = {
				id: document.getElementById("addressId").value,
				province: document.getElementById("province").value,
				city: document.getElementById("city").value,
				county: document.getElementById("county").value,
				detailed: document.getElementById("detailed").value,
				isDefault: document.getElementById("isDefaultInput").value,
				receivingName: document.getElementById("receivingName").value,
				receivingPhone: document.getElementById("receivingPhone").value,
				postCode: document.getElementById("postCode").value,
			};
			mui.ajax(ip + '/address/update', {
				data: JSON.stringify(addressEntity),
				dataType: 'json',
				type: 'post',
				timeout: 10000,
				headers: {
					'Content-Type': 'application/json'
				},
				success: function(data) {
					if(data.status == OPERATION_SUCCESS) {
						alert(data.data.msg);
					}
				},
				error: function(xhr, type, errorThrown) {
					//异常处理；
					mui.toast("系统异常！");
				}
			});
		});

		document.getElementById("delete").addEventListener('tap', function() {
			var btnArray = ['否', '是'];
			var aId = document.getElementById("addressId").value;
			mui.confirm('您确认删除此条地址？', '警告', btnArray, function(e) {
				if(e.index == 1) {
					mui.ajax(ip + '/address/del/' + aId, {
						type: 'delete',
						success: function(data) {
							
							current_view.close();
							var status = data.data.code;
							if(status === OPERATION_SUCCESS) {
								mui.toast(data.msg);
								var current_view = plus.webview.currentWebview();
							var parent_view = current_view.opener();
							parent_view.evalJS("mask.close()");
							} else {
								mui.toast(data.msg);
							}
						},
						error: function(xhr, type, errorThrown) {
							//异常处理；
							mui.toast("系统异常！");
						}
					});
				}
			})
		});
	</script>

</html>